package com.feng.controller;

import com.feng.pojo.Course;
import com.feng.service.course.CourseService;
import com.feng.service.course.CourseServiceImpl;
import com.feng.service.elective.ElectiveService;
import com.feng.service.elective.ElectiveServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/choose")
public class StudentServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String realname = req.getParameter("realname");
        String courseID = req.getParameter("courseID");
        int already = Integer.parseInt(req.getParameter("already"));

        ElectiveService electiveService = new ElectiveServiceImpl();
        CourseService courseService = new CourseServiceImpl();
        req.setAttribute("realname",realname);
        if (already < 50) {
            boolean flag = electiveService.addElective(username, courseID, already, courseService);
            List<Course> courses = courseService.queryElective(username);
            req.setAttribute("list", courses);
            if (flag) {
                for (Course cours : courses) {
                    System.out.println(cours);
                }
                req.getRequestDispatcher("jsp/studentElective.jsp").forward(req, resp);
            } else {
                resp.sendRedirect(req.getContextPath()+"/frame");
            }
        } else {
            resp.sendRedirect(req.getContextPath()+"/frame");
        }
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
